//
//  MainTabBarViewController.swift
//  微博（swift）
//
//  Created by 谢曦 on 2017/5/4.
//  Copyright © 2017年 谢曦. All rights reserved.
//

import UIKit

class MainTabBarViewController: UITabBarController {


    lazy var composeButton = UIButton.init(type: .custom)
    
 
   
    override func viewDidLoad() {
        super.viewDidLoad()
        loadChildViewControllers()
        setComposeButton()
       
    }
    
    func composeButtonClick() {
        print(" +   click")
    }
   
}


  // MARK: - 创建子控制器
extension MainTabBarViewController{
    
    
     func loadChildViewControllers(){
        
        var childs = [MainNavViewController]()
        
       
    
        let dictArr = [
            ["title":"首页","clsName":"HomeViewController","imageName":"tabbar_home","highImageName":"tabbar_home_selected"],
            ["title":"消息","clsName":"MessageViewController","imageName":"tabbar_message_center","highImageName":"tabbar_message_center_selected"],
            ["clsName":"BaseViewController"],
            ["title":"发现","clsName":"DiscoverViewController","imageName":"tabbar_discover","highImageName":"tabbar_discover_selected"],
            ["title":"我","clsName":"MyViewController","imageName":"tabbar_profile","highImageName":"tabbar_profile_selected"]
    ]
        
        for i in 0..<dictArr.count {
            let dict = dictArr[i]
            let vc =  childViewController(dict: dict)
           let nav = MainNavViewController.init(rootViewController: vc)
            childs.append(nav)
            
        }
      viewControllers=childs
        
        
        
    }
    

    

    /// - Parameter dict:控制器携带的属性(title,clsName,imageName,highImageName)
    /// - Returns: 子控制器数组
      private func childViewController(dict:[String:String]) -> (BaseViewController){
        
        guard let title = dict["title"],
            let cls = dict["clsName"],
            let image = dict["imageName"],
            let imageHigh = dict["highImageName"],
            //weibo.BaseViewController !!不要中英文混合 不然bundlename会出现错误
            let vcType =  NSClassFromString(Bundle.main.namespace + "." + cls) as? BaseViewController.Type

            else{
                    return BaseViewController()
            }
           
        let vc = vcType.init()
        vc.tabBarItem.title = title
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.lightGray], for: .normal)
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orange], for: .selected)
        vc.tabBarItem.image = UIImage.init(named: image)?.withRenderingMode(.alwaysOriginal)
        vc.tabBarItem.selectedImage = UIImage.init(named: imageHigh)?.withRenderingMode(.alwaysOriginal)
        vc.title = title
        return vc
        
    }

     // MARK: - 中间的加号按钮
    func setComposeButton()  {
        composeButton.setImage(UIImage.init(named: "tabbar_compose_icon_add"), for: .normal)
        composeButton.setBackgroundImage(UIImage.init(named: "tabbar_compose_button"), for: .normal)
        composeButton.setImage(UIImage.init(named: "tabbar_compose_icon_add_highlighted"), for: .highlighted)
        composeButton.setBackgroundImage(UIImage.init(named: "tabbar_compose_button_highlighted"), for: .highlighted)
        composeButton.addTarget(self
            , action: #selector(composeButtonClick), for: .touchUpInside)
        tabBar.addSubview(composeButton)
        //左右上下缩进
        let w =  tabBar.width/5.0  - 1.0//少缩进一点 可以遮盖住盲点
        composeButton.frame=tabBar.bounds.insetBy(dx: w*2, dy: 0) //左右缩进两个单位
    }
    

}
